<template>
  <div class="hm-login">
    <div class="title">面经登录</div>
    <div class="name">
      <span>用户名</span
      ><input type="text" placeholder="用户名" v-model="username" />
    </div>

    <div class="passward">
      <span>密码</span
      ><input type="text" placeholder="密码" v-model="password" />
    </div>
    <div class="button" @click="login">提交</div>
    <router-link to="/register">注册账号</router-link>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  created() {},
  methods: {
    login() {
      if (this.username.length < 5) {
        alert("用户名长度不能小于5位");
        return;
      }
      if (this.password.length < 6) {
        alert("密码长度不能小于6位");
        return;
      }
      // console.log(this.username.length)
      axios({
        url: "http://interview-api-t.itheima.net/h5/user/login",
        method: "post",
        data: { username: this.username, password: this.password },
        headers: {
          "Content-Type": "application/json",
        },
      })
        .then((res) => {
          // console.log(res)
          // console.log(this.username)

          if (res.status === 200) {
            localStorage.setItem("token", res.data.data.token);
            this.$router.push("/layout");
            // this.$router.push({path:'/layout/user/'+this.username})
            // this.$router.push({name: 'HmLogin', params: { username: this.username }})
            alert("登录成功");
          }
        })
        .catch(() => {
          alert("登录失败");
        }); 
    },
  },
};
</script>

<style lang="less" scoped>
.hm-login {
  width: 100%;
  position: relative;
  .title {
    text-align: center;
    margin: 10px auto;
    line-height: 46px;
    font-weight: 600;
    font-size: 20px;
  }
  .name {
    height: 44px;
    border-bottom: 1px solid #ebedf0;
    display: flex;
    align-items: center;
    padding: 10px 16px;
    span {
      margin-right: 10px;
      font-size: 20px;
      width: 90px;
      height: 24px;
      // background-color: rebeccapurple;
    }
    input {
      flex: 1;
      width: 244px;
      height: 24px;
      border: none;
      outline: none;
    }
  }
  .passward {
    height: 44px;
    border-bottom: 1px solid #ebedf0;
    display: flex;
    align-items: center;
    padding: 10px 16px;
    span {
      margin-right: 10px;
      font-size: 20px;
      width: 90px;
      height: 24px;
      // background-color: rebeccapurple;
    }
    input {
      flex: 1;
      width: 244px;
      height: 24px;
      border: none;
      outline: none;
    }
  }
  .button {
    width: 343px;
    height: 44px;
    background-color: #fa6d1d;
    color: #fff;
    line-height: 44px;
    text-align: center;
    border-radius: 10px;
    margin-top: 20px;
  }
  a {
    height: 16px;
    padding-right: 5.3vw;
    position: absolute;
    right: 0;
    top: 270px;
    font-size: 3.2vw;
  }
}
</style>
